home *** CD-ROM | disk | FTP | other *** search
/ Freelog 22 / freelog 22.iso / Prog / Djgpp / GPC2952B.ZIP / lib / gcc-lib / djgpp / 2.952 / units / trap.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-02-08  |  4.9 KB  |  159 lines

  1. {
  2. Trapping runtime errors
  3.  
  4. The Trap unit allows you to trap runtime errors, so a runtime error
  5. will not abort the program, but pass the control back to a point
  6. within the program.
  7.  
  8. The usage is simple. The TrapExec procedure can be called with a
  9. function (p) as an argument. p must take a Boolean argument. p will
  10. immediately be called with False given as its argument. When a
  11. runtime error would otherwise be caused while p is active, p will
  12. instead be called again with True as its argument. After p returns,
  13. runtime errors will not be trapped.
  14.  
  15. When the program terminates (e.g. by reaching its end or by a Halt
  16. statement) and a runtime error was trapped during the run, Trap will
  17. set the ExitCode and ErrorAddr variables to indicate the trapped
  18. error.
  19.  
  20. Notes:
  21.  
  22. - After trapping a runtime error, your program might not be in a
  23.   stable state. If the runtime error was a "minor" one (such as a
  24.   range checking or arithmetic error), it should not be a problem.
  25.   But if you, e.g., write a larger application and use Trap to
  26.   prevent a sudden abort caused by an unexpected runtime error, you
  27.   should make the program terminate regularly as soon as possible
  28.   after a trapped error (perhaps by telling the user to save the
  29.   data, then terminate the program and report the bug to you).
  30.  
  31. - Since the trapping mechanism *jumps* back, it has all the negative
  32.   effects that a (non-local!) `goto' can have! You should be aware
  33.   of the consequences of all active procedures being terminated at
  34.   an arbitrary point!
  35.  
  36. - Nested traps are supported, i.e. you can call TrapExec again
  37.   within a routine called by another TrapExec instance. Runtime
  38.   errors trapped within the inner TrapExec invocation will be
  39.   trapped by the inner TrapExec, while runtime errors trapped after
  40.   its termination will be trapped by the outer TrapExec again.
  41.  
  42. Copyright (C) 1996-2001 Free Software Foundation, Inc.
  43.  
  44. Author: Frank Heckenbach <frank@pascal.gnu.de>
  45.  
  46. This file is part of GNU Pascal.
  47.  
  48. GNU Pascal is free software; you can redistribute it and/or modify
  49. it under the terms of the GNU General Public License as published by
  50. the Free Software Foundation; either version 2, or (at your option)
  51. any later version.
  52.  
  53. GNU Pascal is distributed in the hope that it will be useful,
  54. but WITHOUT ANY WARRANTY; without even the implied warranty of
  55. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  56. GNU General Public License for more details.
  57.  
  58. You should have received a copy of the GNU General Public License
  59. along with GNU Pascal; see the file COPYING. If not, write to the
  60. Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  61. 02111-1307, USA.
  62.  
  63. As a special exception, if you link this file with files compiled
  64. with a GNU compiler to produce an executable, this does not cause
  65. the resulting executable to be covered by the GNU General Public
  66. License. This exception does not however invalidate any other
  67. reasons why the executable file might be covered by the GNU General
  68. Public License.
  69. }
  70.  
  71. {$gnu-pascal,B-,I-}
  72.  
  73. unit Trap;
  74.  
  75. interface
  76.  
  77. uses GPC;
  78.  
  79. var
  80.   TrappedExitCode : Integer = 0;
  81.   TrappedErrorAddr : Pointer = nil;
  82.   TrappedErrorMessageString : TString = '';
  83.  
  84. { Trap runtime errors. See the comment at the top. }
  85. procedure TrapExec (procedure p (Trapped : Boolean)); asmname '_p_trapexec';
  86.  
  87. { Forget about saved errors from the innermost TrapExec instance. }
  88. procedure TrapReset; asmname '_p_trapreset';
  89.  
  90. implementation
  91.  
  92. {$L trapc.c}
  93. procedure DoSetJmp (procedure p (Trapped : Boolean)); asmname 'dosetjmp';
  94. procedure DoLongJmp; asmname 'dolongjmp';
  95.  
  96. var
  97.   TrapCount : Integer = 0;
  98.  
  99. procedure TrapExit;
  100. begin
  101.   if ErrorAddr <> nil then
  102.     if TrapCount <> 0 then
  103.       begin
  104.         TrappedExitCode := ExitCode;
  105.         TrappedErrorAddr := ErrorAddr;
  106.         TrappedErrorMessageString := ErrorMessageString;
  107.         ErrorAddr := nil;
  108.         ExitCode := 0;
  109.         ErrorMessageString := '';
  110.         DoLongJmp
  111.       end
  112.     else
  113.   else
  114.     if TrappedErrorAddr <> nil then
  115.       begin
  116.         ExitCode := TrappedExitCode;
  117.         ErrorAddr := TrappedErrorAddr;
  118.         ErrorMessageString := TrappedErrorMessageString;
  119.         TrappedErrorAddr := nil
  120.       end
  121. end;
  122.  
  123. procedure TrapExec (procedure p (Trapped : Boolean));
  124. var
  125.   SavedTrappedExitCode : Integer;
  126.   SavedTrappedErrorAddr : Pointer;
  127.   SavedTrappedErrorMessageString : TString;
  128.  
  129.   procedure DoCall (Trapped : Boolean);
  130.   begin
  131.     AtExit (TrapExit);
  132.     p (Trapped)
  133.   end;
  134.  
  135. begin
  136.   SavedTrappedExitCode := TrappedExitCode;
  137.   SavedTrappedErrorAddr := TrappedErrorAddr;
  138.   SavedTrappedErrorMessageString := TrappedErrorMessageString;
  139.   Inc (TrapCount);
  140.   DoSetJmp (DoCall);
  141.   Dec (TrapCount);
  142.   if TrappedErrorAddr = nil then
  143.     begin
  144.       TrappedExitCode := SavedTrappedExitCode;
  145.       TrappedErrorAddr := SavedTrappedErrorAddr;
  146.       TrappedErrorMessageString := SavedTrappedErrorMessageString
  147.     end;
  148.   AtExit (TrapExit)
  149. end;
  150.  
  151. procedure TrapReset;
  152. begin
  153.   TrappedExitCode := 0;
  154.   TrappedErrorAddr := nil;
  155.   TrappedErrorMessageString := ''
  156. end;
  157.  
  158. end.
  159.